package com.tree.xinhe.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.tree.xinhe.CallBack;
import com.tree.xinhe.R;
import com.tree.xinhe.adapter.GridAdapter;
import com.tree.xinhe.adapter.ListAdapter;
import com.tree.xinhe.entity.AdImg;
import com.tree.xinhe.fragment.HeadFragment;
import com.tree.xinhe.util.RequestUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements CallBack{

    private FragmentManager fm;
    private FragmentTransaction ft;
    private HeadFragment headFragment;
    private ListView listView;
    private List<AdImg> list = new ArrayList<>();
    private BaseAdapter listAdapter;
    private List<AdImg> list1 = new ArrayList<>();
    private List<AdImg> list2 = new ArrayList<>();
    private List<AdImg> list3 = new ArrayList<>();
    private List<AdImg> list4 = new ArrayList<>();
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private RecyclerView recyclerView3;
    private RecyclerView.Adapter radapter1;
    private RecyclerView.Adapter radapter2;
    private RecyclerView.Adapter radapter3;

    private RequestUtil util;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        recyclerView1 = (RecyclerView) findViewById(R.id.recycle1);
        recyclerView2 = (RecyclerView) findViewById(R.id.recycle2);
        recyclerView3 = (RecyclerView) findViewById(R.id.recycle3);
        addHeadFragment();

        listAdapter = new ListAdapter(this,list1);
        listView.setAdapter(listAdapter);

//        网格1
        GridLayoutManager glm=new GridLayoutManager(this,2, LinearLayoutManager.VERTICAL,false);
        recyclerView1.setLayoutManager(glm);
        radapter1=new GridAdapter(this,list2);
        recyclerView1.setAdapter(radapter1);

        //网格2
        GridLayoutManager glm2=new GridLayoutManager(this,2, LinearLayoutManager.VERTICAL,false);
        recyclerView2.setLayoutManager(glm2);
        radapter2=new GridAdapter(this,list3);
        recyclerView2.setAdapter(radapter2);

        GridLayoutManager glm3=new GridLayoutManager(this,3, LinearLayoutManager.VERTICAL,false);
        recyclerView3.setLayoutManager(glm3);
        radapter3=new GridAdapter(this,list4);
        recyclerView3.setAdapter(radapter3);

        getDataByNet();
        getDataByNet2();
    }





    //添加头部的轮播fragment
    private void addHeadFragment(){
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        headFragment = new HeadFragment();
        ft.add(R.id.header,headFragment);
        ft.show(headFragment);
        ft.commit();
    }

    private void getDataByNet(){
        util = new RequestUtil(this,this);
        util.getData(getMap());
    }

    private Map<String,String> getMap(){
        Map<String,String> map = new Hashtable<>();
        map.put("isHot","1");
        return map;
    }

    private void getDataByNet2(){
        util = new RequestUtil(this,this);
        util.getData(getMap2());
    }

    private Map<String,String> getMap2(){
        Map<String,String> map = new Hashtable<>();
        map.put("isHot","0");
        map.put("isTop","0");
        return map;
    }

    @Override
    public void callback(String json) {
//        Log.w("tree","热销"+json);
//        list.clear();
//        list1.clear();
        list2.clear();
        list3.clear();
        list4.clear();
        list.addAll(JSON.parseArray(json,AdImg.class));
        for(AdImg adImg:list){
            if(adImg.getIsTop().equals("yes1")){
                list1.add(adImg);
            }else if(adImg.getIsTop().equals("yes2")){
                list2.add(adImg);
            } else if(adImg.getIsTop().equals("yes3")){
                list3.add(adImg);
            } else if(adImg.getIsTop().equals("no")){
                list4.add(adImg);
            }
        }

        listAdapter.notifyDataSetChanged();
        radapter1.notifyDataSetChanged();
        radapter2.notifyDataSetChanged();
        radapter3.notifyDataSetChanged();

    }

    @Override
    public void errorCallback(VolleyError volleyError) {

    }
}
